package com.achobeta.recruitment.service.impl.student;

import com.achobeta.recruitment.common.ResultCode;
import com.achobeta.recruitment.dao.student.StudentMapper;
import com.achobeta.recruitment.model.State;
import com.achobeta.recruitment.model.Student;
import com.achobeta.recruitment.model.vo.StudentVO;
import com.achobeta.recruitment.service.student.StudentService;
import com.achobeta.recruitment.utils.CheckUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-30
 * Time: 22:26
 */
@Service
@Slf4j
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;


    @Override
    public void initStudent(String openid, Integer manageRemarkId) {
        // 校验
        CheckUtils.checkStringParams(ResultCode.FAILED_PARAMS_OPENID, openid);
        CheckUtils.checkNULL(manageRemarkId);
        // 构造对象
        Student student = new Student();
        student.setOpenid(openid);
        student.setManageRemarkId(manageRemarkId);
        Date date = new Date();
        student.setCreateTime(date);
        student.setUpdateTime(date);
        student.setDeleteState((byte) 0);
        student.setStateId((byte) 0);
        // 插入表
        int rows = studentMapper.insertSelective(student);
        // 校验
        CheckUtils.checkRows(rows, 1, ResultCode.FAILED_UNAUTHORIZED);
        // 成功
        log.info(ResultCode.SUCCESS + "  student id：" + student.getId());
    }

    @Override
    public List<String> getStateList() {
        State state = new State();
        List<String> stateList = state.getStateList();
        return stateList == null ? new ArrayList<>() : stateList;
    }

    @Override
    public List<StudentVO> getStudentList() {
        List<StudentVO> studentVOS = studentMapper.getStudentList();
        studentVOS = studentVOS == null ? new ArrayList<>() : studentVOS;
        log.info(ResultCode.SUCCESS + "  查询到的行数： " + studentVOS.size());
        return studentVOS;
    }

    @Override
    public void updateState(String openid, Byte stateId) {
        // 校验
        CheckUtils.checkStringParams(ResultCode.FAILED_PARAMS_OPENID, openid);
        CheckUtils.checkNULL(stateId);
        State state = new State();
        int size = state.getStateList().size();
        if(stateId.compareTo((byte) size) >= 0 || stateId < 0) {
            CheckUtils.printWarn(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        // 获取学生的id
        Student student = selectStudentByOpenid(openid);
        CheckUtils.checkNULL(student);
        Student updateStudent = new Student();
        updateStudent.setId(student.getId());
        updateStudent.setStateId(stateId);
        updateStudent.setUpdateTime(new Date());
        // 更新
        int rows = studentMapper.updateByPrimaryKeySelective(updateStudent);
        // 校验
        CheckUtils.checkRows(rows, 1, ResultCode.FAILED_STUDENT_STATE_UPDATE);
        // 成功
        log.info(ResultCode.SUCCESS + "  学生id： " + student.getId() + " 状态修改为：" + stateId);
    }


    @Override
    public Student selectStudentByOpenid(String openid) {
        // 校验
        CheckUtils.checkStringParams(ResultCode.FAILED_PARAMS_OPENID, openid);
        // 无论结果如何都返回
        return studentMapper.selectStudentByOpenid(openid);
    }

}
